import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;


public class SetOfIntSets implements Comparable<ConcurrentSkipListSet<Integer>>, 
											Iterable<ConcurrentSkipListSet<Integer>>, Collection<ConcurrentSkipListSet<Integer>>{
	private ConcurrentSkipListSet<ConcurrentSkipListSet<Integer>> superSet;
	
	public SetOfIntSets() {
		superSet = new ConcurrentSkipListSet<ConcurrentSkipListSet<Integer>>();
	}
	
	public static SetOfIntSets possibleSubsets(ConcurrentSkipListSet<Integer> set) {
		final int SUBSETMAXLENGTH=set.size()-1;
		return createSubset(superSet,set,SUBSETMAXLENGTH);
	}
	
	private static SetOfIntSets createSubset( ConcurrentSkipListSet<Integer> set, ConcurrentSkipListSet<Integer> givenSet, int level) {
		SetOfIntSets superSet = new SetOfIntSets();
		ConcurrentSkipListSet<Integer> tempGivenSet = givenSet.clone();
		ConcurrentSkipListSet<Integer> tempSet = set.clone();
		
		for(int i: givenSet) {
			tempSet.add(i);
			if(level >1) {
				tempGivenSet.remove(i);
				superSet.addAll(createSubset(tempSet,tempGivenSet,level-1));
			} else {
				superSet.add(set);
			}
	
		}
		return superSet;
	}

	@Override
	public int compareTo(ConcurrentSkipListSet<Integer> o) {
		return superSet.toString().compareTo(o.toString());
	}

	@Override
	public Iterator iterator() {
		return superSet.iterator();
	}
	@Override
	public boolean add(ConcurrentSkipListSet<Integer> e) {
		return superSet.add(e);
	}
	@Override
	public boolean addAll(Collection<? extends ConcurrentSkipListSet<Integer>> c) {
		return superSet.addAll(c);
	}
	@Override
	public void clear() {
		superSet.clear();
	}
	@Override
	public boolean contains(Object o) {
		return superSet.contains(o);
	}
	@Override
	public boolean containsAll(Collection<?> c) {
		return superSet.containsAll(c); 
	}
	@Override
	public boolean isEmpty() {
		return superSet.isEmpty(); 
	}
	@Override
	public boolean remove(Object o) {
		return superSet.remove(o);
	}
	@Override
	public boolean removeAll(Collection<?> c) {
		return superSet.removeAll(c);
	}
	@Override
	public boolean retainAll(Collection<?> c) {
		return superSet.retainAll(c);
	}
	@Override
	public int size() {
		return superSet.size();
	}
	@Override
	public Object[] toArray() {
		return superSet.toArray();
	}
	@Override
	public <T> T[] toArray(T[] a) {
		return superSet.toArray(a);
	}
}
